package cn.yunhe.controller;


import cn.yunhe.entity.*;
import cn.yunhe.service.IConsigneeService;
import cn.yunhe.service.IGoodsService;
import cn.yunhe.service.IOrderService;
import cn.yunhe.service.ISendPersonService;
import cn.yunhe.util.DateUtil;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet("/before/AddOrderServlet")
public class AddOrderServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String fname = request.getParameter("fname");
        String ftel = request.getParameter("ftel");
        String faddress = request.getParameter("faddress");

        if ((fname != null || fname != "") && (ftel != null || ftel != "") && (faddress != null || faddress != "")) {

            SendPerson sendPerson = new SendPerson(fname, ftel, faddress);
            ISendPersonService iSendPersonService = new ISendPersonService();

            int flag1 = iSendPersonService.addSendPerson(sendPerson);
            int fid = sendPerson.getFid();

            String cname = request.getParameter("cname");
            String phone1 = request.getParameter("phone1");
            String address = request.getParameter("address");
            if ((cname != null || cname != "") && (phone1 != null || phone1 != "") && (address != null || address != "")) {

                Consignee consignee = new Consignee(cname, address, phone1);
                IConsigneeService iConsigneeService = new IConsigneeService();
                int flag2 = iConsigneeService.addConsignee(consignee);
                int consigneeId = consignee.getConsigneeId();
                String gname = request.getParameter("gname");
                String weight = request.getParameter("weight");
                String size = request.getParameter("size");

                Goods goods = new Goods(gname, weight, size);
                IGoodsService iGoodsService = new IGoodsService();
                int flag3 = iGoodsService.addGoods(goods);
                IOrderService iOrderService = new IOrderService();

                int goodsId = goods.getGoodsId();
                String date = DateUtil.dateTimeToString(new Date());
                String typeIdStr = request.getParameter("typeId");
                String typeName = request.getParameter("typeName");
                int typeId = Integer.parseInt(typeIdStr);
                Type type = new Type(typeId, typeName);
                SendPerson sendPerson1 = new SendPerson(fid);
                Consignee consignee1 = new Consignee(consigneeId);
                Goods goods1 = new Goods(goodsId);
                Order order = new Order(date, sendPerson1, consignee1, goods1, type);

                int flag4 = iOrderService.addOrder(order);
                if (flag1 > 0 && flag2 > 0 && flag3 > 0 && flag4 > 0) {
                    out.print("true");
                } else {
                    out.print("false");
                }
            }

        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

}
